using System;
using System.Globalization;
using System.Text;
namespace Net35.Xrm.Sdk
{
	internal static class ClientExceptionHelper
	{
		internal static void ThrowIfNegative(int value, string parameterName)
		{
			if (value < 0)
			{
				throw new ArgumentOutOfRangeException(parameterName, value, "Value for this parameter must be equal or greater than zero.");
			}
		}
		internal static void ThrowIfNull(object parameter, string name)
		{
			if (parameter == null)
			{
				throw new ArgumentNullException(name);
			}
		}
		internal static void ThrowIfNullOrEmpty(string parameter, string name)
		{
			if (string.IsNullOrEmpty(parameter))
			{
				throw new ArgumentNullException(name);
			}
		}
		internal static void ThrowIfGuidEmpty(Guid parameter, string name)
		{
			if (parameter == Guid.Empty)
			{
				throw new ArgumentException("Expected non-empty Guid.", name);
			}
		}
		internal static string FormatMessage(int errorCode, params object[] arguments)
		{
			string result;
			if (errorCode == 0)
			{
				result = ClientExceptionHelper.BuildErrorTable(string.Empty, arguments);
			}
			else
			{
				string text = errorCode.ToString(CultureInfo.InvariantCulture);
				try
				{
					result = string.Format(CultureInfo.InvariantCulture, text, arguments);
				}
				catch (FormatException)
				{
					result = ClientExceptionHelper.BuildErrorTable(text, arguments);
				}
			}
			return result;
		}
		private static string BuildErrorTable(string message, object[] arguments)
		{
			StringBuilder stringBuilder = new StringBuilder(message);
			for (int i = 0; i < arguments.Length; i++)
			{
				stringBuilder.Append(string.Format(CultureInfo.InvariantCulture, "\nData[{0}] = \"{1}\"", new object[]
				{
					i,
					arguments[i]
				}));
			}
			return stringBuilder.ToString();
		}
		internal static void Assert(bool condition, string message, params object[] args)
		{
			ClientExceptionHelper.Assert(condition, string.Format(CultureInfo.InvariantCulture, message, args));
		}
		internal static void Assert(bool condition, string message)
		{
			InvalidOperationException ex = null;
			if (!condition)
			{
				ex = new InvalidOperationException(message);
			}
			if (ex != null)
			{
				throw ex;
			}
		}
	}
}
